package com.jeecg.p3.system.advisor.web;

import com.jeecg.p3.base.util.DateUtil;
import com.jeecg.p3.system.advisor.entity.StuAdvGroup;
import com.jeecg.p3.system.advisor.entity.StudyAdviser;
import com.jeecg.p3.system.advisor.service.StuAdvGroupService;
import com.jeecg.p3.system.advisor.service.StudyAdvService;
import com.jeecg.p3.system.base.BaseController;
import com.jeecg.p3.system.entity.JwSystemUser;
import com.jeecg.p3.system.util.Constants;
import com.jeecg.p3.system.vo.LoginUser;
import com.jeecg.p3.weixin.entity.WeixinGzuser;
import com.jeecg.p3.weixin.service.WeixinGzuserService;
import org.apache.velocity.VelocityContext;
import org.jeecgframework.p3.core.common.utils.AjaxJson;
import org.jeecgframework.p3.core.util.SystemTools;
import org.jeecgframework.p3.core.util.plugin.ViewVelocity;
import org.jeecgframework.p3.core.utils.common.PageQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 学习顾问管理控制层
 * Created on 2020-08-29$ 9:00$
 *
 * @author $张晓
 * @version V1.0
 */
@Controller
@RequestMapping("/system/back/advisor")
public class StudyAdvController extends BaseController {
    public final Logger log = LoggerFactory.getLogger(getClass());
    @Autowired
    private StudyAdvService service;
    @Autowired
    private StuAdvGroupService advGroupService;
    @Autowired
    private WeixinGzuserService weixinGzuserService;

    /**
     *
     * @param query 群组查询对象
     * @param request
     * @param response
     * @param pageNo 第几页
     * @param pageSize 页数大小
     * @throws Exception
     */
    @RequestMapping(value="/list/list.do",method = {RequestMethod.GET,RequestMethod.POST})
    public void toIndividual(@ModelAttribute StudyAdviser query, HttpServletRequest request, HttpServletResponse response,
                             @RequestParam(required = false, value = "pageNo", defaultValue = "1") int pageNo,
                             @RequestParam(required = false, value = "pageSize", defaultValue = "10") int pageSize) throws Exception{
        VelocityContext velocityContext = new VelocityContext();
        LoginUser user = getCurrentUser(request);
        JwSystemUser systemUser = this.userService.queryById(Long.valueOf(user.getId()));
        if(Constants.stateActvi.equals(systemUser.getAdmin())) {
            velocityContext.put("admin",true);//设置管理员
        } else {
            query.setUserNumber(systemUser.getUserId());
        }
        PageQuery<StudyAdviser> pageQuery = new PageQuery<StudyAdviser>();
        pageQuery.setPageNo(pageNo);
        pageQuery.setPageSize(pageSize);

        pageQuery.setQuery(query);
        velocityContext.put("adviser",query);
        velocityContext.put("pageInfos", SystemTools.convertPaginatedList(service.queryPageList(pageQuery)));
        String viewName = "system/back/advisor/stuAdvList.vm";
        ViewVelocity.view(request,response,viewName,velocityContext);
    }

    /**
     * 跳转到新增群组界面页面
     * @return
     */
    @RequestMapping(value = "/list/toAdd",method ={RequestMethod.GET, RequestMethod.POST})
    public void toAddDialog(HttpServletRequest request, HttpServletResponse response, ModelMap model)throws Exception{
        VelocityContext velocityContext = new VelocityContext();
        StuAdvGroup stuAdvGroup = new StuAdvGroup();
        List<StuAdvGroup> groups = this.advGroupService.findList(stuAdvGroup);
        velocityContext.put("groups",groups);
        String jwid=request.getSession().getAttribute("jwid").toString();
        PageQuery<WeixinGzuser> pageQuery = new PageQuery<WeixinGzuser>();
        pageQuery.setPageNo(1);
        pageQuery.setPageSize(10000);
        WeixinGzuser weixinGzuser = new WeixinGzuser();
        pageQuery.setQuery(weixinGzuser);
        List<WeixinGzuser> gzusers = weixinGzuserService.queryPageList(pageQuery).getValues();
        velocityContext.put("gzusers",gzusers);
        String viewName = "system/back/advisor/stuAdv_add.vm";
        ViewVelocity.view(request,response,viewName,velocityContext);
    }

    /**
     * 保存信息
     * @return
     */
    @RequestMapping(value = "/list/doAdd",method ={RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public AjaxJson doAdd(@ModelAttribute StudyAdviser adviser, HttpServletRequest request){
        AjaxJson j = new AjaxJson();
        try {
            LoginUser user = getCurrentUser(request);
            Boolean repeat = userService.validReatUserId(adviser.getUserNumber(),null);
            if (repeat){
                j.setSuccess(false);
                j.setMsg("用户编码重复,重新输入!");
            } else {
                adviser.setCreateDate(DateUtil.dateToString(new Date()));
                adviser.setCreateTime(DateUtil.dateToString(new Date(), "HH:mm:ss"));
                adviser.setCreateUser(user.getId());
                adviser.setStatus("1");
                adviser.setIsAct("0");//设置顾问未激活
                service.doAdd(adviser);
                j.setMsg("保存成功");
            }
        } catch (Exception e) {
            log.info(e.getMessage());
            j.setSuccess(false);
            j.setMsg("保存失败");
        }
        return j;
    }

    /**
     * 跳转到编辑页面
     * @return
     */
    @RequestMapping(value="/list/toEdit",method = RequestMethod.GET)
    public void toEdit(@RequestParam(required = true, value = "id" ) Long id,HttpServletResponse response,HttpServletRequest request) throws Exception{
        VelocityContext velocityContext = new VelocityContext();
        StudyAdviser adviser = service.findById(id);
        StuAdvGroup stuAdvGroup = new StuAdvGroup();
        List<StuAdvGroup> groups = this.advGroupService.findList(stuAdvGroup);
        velocityContext.put("groups",groups);
        velocityContext.put("adviser",adviser);
        String viewName = "system/back/advisor/stuAdv_edit.vm";
        ViewVelocity.view(request,response,viewName,velocityContext);
    }

    /**
     * 保存修改数据
     * @return
     */
    @RequestMapping(value = "/list/doEdit",method ={RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public AjaxJson doEdit(@ModelAttribute StudyAdviser adviser, HttpServletRequest request){
        AjaxJson j = new AjaxJson();
        try {
            LoginUser user = getCurrentUser(request);
            adviser.setUpdateDate(DateUtil.dateToString(new Date()));
            adviser.setUpdateTime(DateUtil.dateToString(new Date(), "HH:mm:ss"));
            adviser.setUpdateUser(user.getId());
            this.service.doEdit(adviser);
            j.setMsg("编辑成功");
        } catch (Exception e) {
            e.printStackTrace();
            log.info(e.getMessage());
            j.setSuccess(false);
            j.setMsg("编辑失败");
        }
        return j;
    }

    /**
     * 删除
     * @return
     */
    @RequestMapping(value="/list/doDelete",method = RequestMethod.GET)
    @ResponseBody
    public AjaxJson doDelete(@RequestParam(required = true, value = "id" ) Long id, HttpServletRequest request){
        AjaxJson j = new AjaxJson();
        try {
            LoginUser user = getCurrentUser(request);
            StudyAdviser adviser = service.findById(id);
            adviser.setDeleteDate(DateUtil.dateToString(new Date()));
            adviser.setDeleteTime(DateUtil.dateToString(new Date(), "HH:mm:ss"));
            adviser.setDeleteUser(user.getId());
            service.doDelete(adviser);
            j.setMsg("删除成功");
        } catch (Exception e) {
            log.info(e.getMessage());
            j.setSuccess(false);
            j.setMsg("删除失败:"+e.getMessage());
        }
        return j;
    }

}
